﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using HotDealD2.Models.ModelView;
using HotDealD2.Models.ModelManager;
using HotDealD2.Models;
using HotDealD2.Helper;

namespace HotDealD2.Controllers
{
    [Authorize(Roles = "Admin")]
    public class AdminController : Controller
    {
        //
        // GET: /Admin/
        
        public ActionResult Index()
        {
            DealModel dealmodel = new DealModel();
            IList<Deal> listdeal = dealmodel.GetAllDeal();
           
            return View("Deal/Index",listdeal);
        }
        [HttpPost]
        public ActionResult Index(string searchString)
        {

            DealModel dealmodel = new DealModel();
            IList<Deal> listdeal = dealmodel.GetAllDeal();
            if (!String.IsNullOrEmpty(searchString))
            {
                listdeal = dealmodel.SearchDeal(searchString);
            }

            return View("Deal/Index", listdeal);
        }
        public ActionResult IndexInsertDeal()
        {
            DealModel dealmodel = new DealModel();
            IList<DealDaDuyet> dealdaduyet = dealmodel.GetAllDealDaDuyet();
            return View("Deal/IndexInsertDeal", dealdaduyet);
        }
        [HttpPost]
        public ActionResult IndexInsertDeal(string searchString)
        {

            DealModel dealmodel = new DealModel();
            IList<DealDaDuyet> listdeal = dealmodel.GetAllDealDaDuyet();
            if (!String.IsNullOrEmpty(searchString))
            {
                listdeal = dealmodel.SearchDealDaDuyet(searchString);
            }

            return View("Deal/IndexInsertDeal", listdeal);
        }
        public ActionResult InsertDeal(string tenDeal)
        {
            DealModel dealmodel = new DealModel();
            DealDaDuyet deal = dealmodel.GetDealDaDuyet(tenDeal);
            return View("Deal/InsertDeal",deal);
        }

        [HttpPost]
        public ActionResult InsertDeal(DealDaDuyet deal)
        {
            DealModel dealmodel = new DealModel();
            bool result =dealmodel.InsertDeal(deal);
            if (result == true)
            {
                dealmodel.DeleteDealDaDuyet(deal.TenDeal);
                return RedirectToAction("IndexInsertDeal", "Admin");
            }
            return View("Deal/InsertDeal");
        }

        public ActionResult DeleteDeal(int id)
        {
            DealModel dealmodel = new DealModel();
            Deal deal = dealmodel.GetDeal(id);
            return View("Deal/DeleteDeal",deal);
        }
        [HttpPost]
        public ActionResult DeleteDeal(Deal deal,int id)
        {
            DealModel dealmodel = new DealModel();
            bool result = dealmodel.DeleteDeal(id);
            if (result == true)
            {
              
                return RedirectToAction("Index", "Admin");
              
            }
            return View("Deal/DeleteDeal");
        }

        public ActionResult UpdateDeal(int id)
        {
            DealModel dealmodel = new DealModel();
            Deal deal = dealmodel.GetDeal(id);
            return View("Deal/UpdateDeal",deal);
        }
        [HttpPost]
        public ActionResult UpdateDeal(Deal temp,int id)
        {
            DealModel dealmodel = new DealModel();
            bool result = dealmodel.UpdateDeal(temp);
            if (result == true)
            {
                return RedirectToAction("Index", "Admin");
              
            }

            return View("Deal/UpdateDeal");
        }
        public ActionResult IndexApproveDeal()
        {
            DealModel dealmodel = new DealModel();
            IList<DealChuaDuyet> dealchuaduyet = dealmodel.GetAllDealChuaDuyet();
            return View("Deal/IndexApproveDeal", dealchuaduyet);
        }
        [HttpPost]
        public ActionResult IndexApproveDeal(string searchString)
        {

            DealModel dealmodel = new DealModel();
            IList<DealChuaDuyet> listdeal = dealmodel.GetAllDealChuaDuyet();
            if (!String.IsNullOrEmpty(searchString))
            {
                listdeal = dealmodel.SearchDealChuaDuyet(searchString);
            }

            return View("Deal/IndexApproveDeal", listdeal);
        }

        public ActionResult ApproveDeal(string tenDeal)
        {
            DealModel dealmodel = new DealModel();
            DealChuaDuyet dealchuaduyet = dealmodel.GetDealChuaDuyet(tenDeal);
            return View("Deal/ApproveDeal",dealchuaduyet);
        }
        [HttpPost]
        public ActionResult ApproveDeal(DealChuaDuyet deal)
        {
            DealModel dealmodel = new DealModel();
            bool result = dealmodel.InsertDealDaDuyet(deal);
            if (result == true)
            {
                dealmodel.DeleteDealChuaDuyet(deal.TenDeal);
                return RedirectToAction("IndexApproveDeal", "Admin");

            }
            return View("Deal/ApproveDeal");
        }

        
        
        public ActionResult IndexCustomer()
        {
            
            AccountModel account = new AccountModel();
            IList<ThanhVien> listthanhvien = account.GetListAllMemberAccount();
           
            return View("Customer/IndexCustomer",listthanhvien);
        }
        [HttpPost]
        public ActionResult IndexCustomer(string searchString)
        {

            AccountModel account = new AccountModel();
            IList<ThanhVien> listthanhvien = account.GetListAllMemberAccount();
            if (!String.IsNullOrEmpty(searchString))
            {
                listthanhvien = account.SearchAccount(searchString);
            }

            return View("Customer/IndexCustomer", listthanhvien);
        }

     

        public ActionResult InsertCustomer()
        {
            return View("Customer/InsertCustomer");
        }
        [HttpPost]
        public ActionResult InsertCustomer(RegisterView regisView)
        {

            if (!ModelState.IsValid)
                return View("Customer/InsertCustomer");
            else
            {
                AccountModel account = new AccountModel();
                if (!account.IsUserLoginIDExist(regisView.UserName))
                {
                    if (!account.IsEmailAddressExist(regisView.Email))
                    {
                        account.InsertAccount(regisView);
                        return RedirectToAction("IndexCustomer", "Admin");
                    }
                    else
                    {
                        ModelState.AddModelError("", "Địa chỉ E-mail đã tồn tại");

                    }
                }
                else
                {
                    ModelState.AddModelError("", "Tên tài khoản đã tồn tại");
                }


                return View("Customer/InsertCustomer");
            }
        }
        
        public ActionResult DeleteCustomer(String username)
        {
            AccountModel account = new AccountModel();
            ThanhVien thanhvien = account.GetAccount(username);
            return View("Customer/DeleteCustomer",thanhvien);
        }
        [HttpPost]
        public ActionResult DeleteCustomer(ThanhVien thanhvien, String username)
        {
            AccountModel account = new AccountModel();
            bool result = account.DeleteAccount(username);
            if (result == true)
            {
                return RedirectToAction("IndexCustomer", "Admin");
            }
            return View("Customer/DeleteCustomer");
        }

        public RedirectToRouteResult CustomerToBusiness(String username)
        {
           
            AccountModel account = new AccountModel();
            ThanhVien thanhvien = account.GetAccount(username);
            if (thanhvien.DangKyDoanhNghiep == true)
            {
                bool result = account.UpdateAccountToBusiness(thanhvien);
                return RedirectToAction("IndexCustomer", "Admin");
            }

           return  RedirectToAction("IndexCustomer", "Admin");
        }
        public ActionResult IndexOrder()
        {
            var list = new List<String>();
            OrderDealModel ordermodel = new OrderDealModel();
            IList<OrderDealView> listDonHang = ordermodel.GetListOrderPromotion();
            
            return View("Order/IndexOrder",listDonHang);
        }

        public ActionResult DeleteOrder(int id)
        {
            var list = new List<String>();
            
            OrderDealModel ordermodel = new OrderDealModel();
         
            OrderDealView listDonHang = ordermodel.GetDonHang(id);

            return View("Order/DeleteOrder", listDonHang);
        }
        [HttpPost]
        public ActionResult DeleteOrder(OrderDealView temp,int id)
        {
            var list = new List<String>();
            OrderDealModel ordermodel = new OrderDealModel();
            bool result = ordermodel.DeleteOrder(id);
            if (result)
            {
                return RedirectToAction("IndexOrder", "Admin");
            }

            return View("Order/DeleteOrder");
        }

        public ActionResult UpdateOrder(int id)
        {
            var list = new List<String>();
            OrderDealModel ordermodel = new OrderDealModel();
            OrderDealView listDonHang = ordermodel.GetDonHang(id);

            return View("Order/UpdateOrder", listDonHang);
        }
        [HttpPost]
        public ActionResult UpdateOrder(OrderDealView temp,int id)
        {
            var list = new List<String>();
            OrderDealModel ordermodel = new OrderDealModel();
            bool result = ordermodel.UpdateOrder(temp);
            if (result)
            {
                return RedirectToAction("IndexOrder", "Admin");
            }


            return View("Order/UpdateOrder");
        }
        public ActionResult IndexBusiness()
        {
            AccountModel account = new AccountModel();
            IList<ThanhVien> listthanhvien = account.GetListAllBusinessAccount();
         

            return View("Business/IndexBusiness", listthanhvien);
          
        }
        [HttpPost]
        public ActionResult IndexBusiness(String searchString)
        {
            AccountModel account = new AccountModel();
            IList<ThanhVien> listthanhvien = account.GetListAllBusinessAccount();
            if (!String.IsNullOrEmpty(searchString))
            {
                listthanhvien = account.SearchAccount(searchString);
            }

            return View("Business/IndexBusiness", listthanhvien);

        }

        public ActionResult InsertBusiness()
        {
            return View("Business/InsertBusiness");
        }
        [HttpPost]
        public ActionResult InsertBusiness(RegisterView regisView)
        {
            AccountModel account = new AccountModel();
            if (!account.IsUserLoginIDExist(regisView.UserName))
            {
                if (!account.IsEmailAddressExist(regisView.Email))
                {
                    account.InsertBusiness(regisView);
                    return RedirectToAction("IndexBusiness", "Admin");
                }
                else
                {
                    ModelState.AddModelError("", "Địa chỉ E-mail đã tồn tại");

                }
            }
            else
            {
                ModelState.AddModelError("", "Ten Tai Khoan Da Ton Tai");
            }
            
            return View("Business/InsertBusiness");
        }
        public ActionResult UpdateBusiness(string username)
        {
            AccountModel account = new AccountModel();
            ThanhVien thanhvien = account.GetAccount(username);
            return View("Business/UpdateBusiness",thanhvien);
        }
        [HttpPost]
        public ActionResult UpdateBusiness(ThanhVien thanhvien,string username)
        {
            AccountModel account = new AccountModel();
            bool result = account.UpdateAccount(thanhvien);
            if (result == true)
            {
                return RedirectToAction("IndexBusiness", "Admin");
            }
            return View("Business/UpdateBusiness");
        }
        public ActionResult DeleteBusiness(String username)
        {
            AccountModel account = new AccountModel();
            ThanhVien thanhvien = account.GetAccount(username);
            return View("Business/DeleteBusiness",thanhvien);
        }
        [HttpPost]
        public ActionResult DeleteBusiness(ThanhVien thanhvien,String username)
        {
            AccountModel account = new AccountModel();
            bool result = account.DeleteBusiness(username);
            if (result == true)
            {
                return RedirectToAction("IndexBusiness","Admin");
            }
            return View("Business/DeleteBusiness");
        }
        public ActionResult DeleteComment(int id)
        {
            CommentModel commentmodel = new CommentModel();
            Comment comment = commentmodel.GetComment(id);
            return View("Comment/DeleteComment",comment);
        }
        [HttpPost]
        public ActionResult DeleteComment(Comment tempcomment,int id)
        {
            CommentModel commentmodel = new CommentModel();
            bool result = commentmodel.DeleteComment(id);
            if (result)
            {
                return RedirectToAction("IndexComment", "Admin");
            }
            return View("Comment/DeleteComment");
        }
        public ActionResult IndexComment()
        {
           
            CommentModel commentmodel = new CommentModel();
            IList<Comment> listcomment = commentmodel.GetAllComment();
            
            
            return View("Comment/IndexComment",listcomment);
        }
        [HttpPost]
        public ActionResult IndexComment(string searchString)
        {
            CommentModel commentmodel = new CommentModel();
            IList<Comment> listcomment = commentmodel.GetAllComment();
            if (!String.IsNullOrEmpty(searchString))
            {
                listcomment = commentmodel.SearchComment(searchString);
            }
            return View("Comment/IndexComment", listcomment);
        }

        public ActionResult PostComment()
        {
            return View("Comment/PostComment");
        }
        [HttpPost]
        public ActionResult PostComment(DealChuaDuyet deal)
        {
            DealModel dealmodel = new DealModel();
            bool result = dealmodel.InsertDealDaDuyet(deal);
            if (result == true)
            {
                dealmodel.DeleteDealChuaDuyet(deal.TenDeal);
                return RedirectToAction("IndexApproveDeal", "Admin");

            }
            return View("Comment/PostComment");
        }

        
        public ActionResult UpdateComment(int id)
        {
            CommentModel commentmodel = new CommentModel();
            Comment comment = commentmodel.GetComment(id);
            return View("Comment/UpdateComment", comment);
        }
        [HttpPost]
        public ActionResult UpdateComment(Comment tempcomment)
        {
            CommentModel commentmodel = new CommentModel();
            bool result = commentmodel.UpdateComment(tempcomment);
            if(result)
            {
                return RedirectToAction("IndexComment","Admin");
            }
            return View("Comment/UpdateComment");
        }
        public ActionResult IndexStats()
        {
            StatsModel statsmodel = new StatsModel();
            List<StatsView> statsview = new List<StatsView>();
            statsview = statsmodel.CaculatorByDeal();
            return View("Stats/IndexStats",statsview);
        }
        [HttpPost]
        public ActionResult IndexStats(FormCollection form)
        {
            StatsModel statsmodel = new StatsModel();
            List<StatsView> statsview = new List<StatsView>();
            String t = form.Get("thongke");
            if (t.CompareTo("deal") == 0)
            {

                statsview = statsmodel.CaculatorByDeal();
            }
            else
                if (t.CompareTo("doanhnghiep") == 0)
                {
                    statsview = statsmodel.CaculatorByBusiness();
                }
                else
                    if (t.CompareTo("thang") == 0)
                    {
                        statsview = statsmodel.CaculatorByMonth();
                    }
            return View("Stats/IndexStats", statsview);
        }



      

    }
}
